# -*- coding:utf-8 -*-
from tornado import gen
import os

from appcommon.framework import WebBaseHandler
from common import utiltool
from common import timetool
import models as infom
import settings


class PubConsultListHandler(WebBaseHandler):
    """
    一键咨询列表
    """
    @gen.coroutine
    def get(self):
        rows = self.get_argument('rows', '')
        page = self.get_argument('page', '')
        info_list = yield infom.get_pub_consut_list()
        start = (int(page) - 1) * int(rows)
        info = info_list[start:start + int(rows)]
        for item in info:
            item['add_time'] = timetool.convert_timestamp_to_date(item['add_time'])
        res = dict(rows=info, total=len(info_list))
        utiltool.response_write(self, res)


class UserAddSchoolHandler(WebBaseHandler):
    """
    用户提交的驾校列表
    """
    @gen.coroutine
    def get(self):
        rows = self.get_argument('rows', '')
        page = self.get_argument('page', '')
        area = self.get_argument('area', '')
        school = self.get_argument('school', '')
        info_list = yield infom.get_user_add_school_list(area, school)
        start = (int(page) - 1) * int(rows)
        info = info_list[start:start + int(rows)]
        for item in info:
            item['add_time'] = timetool.convert_timestamp_to_date(item['add_time'])
        res = dict(rows=info, total=len(info_list))
        utiltool.response_write(self, res)


class GetDriverSchoolHandler(WebBaseHandler):
    """
    驾校列表
    """
    @gen.coroutine
    def get(self):
        rows = self.get_argument('rows', '')
        page = self.get_argument('page', '')
        area = self.get_argument('area', '')
        school = self.get_argument('school', '')
        info_list = yield infom.get_driver_school_list(area, school)
        start = (int(page) - 1) * int(rows)
        info = info_list[start:start + int(rows)]
        for item in info:
            item['add_time'] = timetool.convert_timestamp_to_date(item['add_time'])
        res = dict(rows=info, total=len(info_list))
        utiltool.response_write(self, res)


class SetDriverSchoolHandler(WebBaseHandler):
    """
    新增驾校
    """
    @gen.coroutine
    def post(self):
        area = self.get_argument('area', '')
        school = self.get_argument('school', '')
        yield infom.add_driver_school(area, school)
        res = {"code": "0", "msg": u"新增成功"}
        utiltool.response_write(self, res)


class DelDriverSchoolHandler(WebBaseHandler):
    """
    删除驾校
    """
    @gen.coroutine
    def post(self):
        id = self.get_argument('id', '')
        yield infom.del_driver_school(id)
        res = {"code": "0", "msg": u"删除成功"}
        utiltool.response_write(self, res)
